package com.barry.download;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.barry.download.http.impl.MyDownloadFileRequest;
import com.barry.download.module.DownloadModule;
import com.barry.download.util.Util;

public class DownloadAdapter extends BaseAdapter
{
    private LayoutInflater inflater;
    private ArrayList<DownloadModule> downloadList;
    private int curPos;
    private OnClickListener mBtnListener;


    public DownloadAdapter(Context context, ArrayList<DownloadModule> list,
            OnClickListener btnListener)
    {
        inflater = LayoutInflater.from(context);
        this.downloadList = list;
        this.mBtnListener = btnListener;
    }


    public void setList(ArrayList<DownloadModule> list)
    {
        this.downloadList = list;
    }


    public void setSelectedPos(int index)
    {
        if (index >= 0 && index < getCount())
        {
            curPos = index;
        }
        else
        {
            curPos = -1;
        }
    }


    public int getSelectedPos()
    {
        return curPos;
    }


    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        if (downloadList == null)
        {
            return 0;
        }

        return downloadList.size();
    }


    @Override
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        ViewHolder holder;

        // 初始化VIEW HOLDER
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.download_item, null);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.btnOpen = (TextView) convertView.findViewById(R.id.btn_open);
            holder.btnPause = (TextView) convertView.findViewById(R.id.btn_pause);
            holder.btnContinue = (TextView) convertView.findViewById(R.id.btn_continue);
            holder.btnDownload = (TextView) convertView.findViewById(R.id.btn_download);

            holder.btnOpenArea = (LinearLayout) convertView.findViewById(R.id.btn_open_area);
            holder.btnPauseArea = (LinearLayout) convertView.findViewById(R.id.btn_pause_area);
            holder.btnContinueArea = (LinearLayout) convertView.findViewById(R.id.btn_continue_area);
            holder.btnDownloadArea = (LinearLayout) convertView.findViewById(R.id.btn_download_area);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        // 设置按钮监听，并将DownloadModule对象保存到按钮的tag中供按钮点击时使用
        DownloadModule dm = downloadList.get(position);
        holder.btnContinueArea.setTag(dm);
        holder.btnDownloadArea.setTag(dm);
        holder.btnPauseArea.setTag(dm);
        holder.btnOpenArea.setTag(dm);

        holder.btnContinueArea.setOnClickListener(mBtnListener);
        holder.btnDownloadArea.setOnClickListener(mBtnListener);
        holder.btnPauseArea.setOnClickListener(mBtnListener);
        holder.btnOpenArea.setOnClickListener(mBtnListener);

        holder.title.setText(dm.getTitle());

        // 临时文件
        String tempFileName = MyDownloadFileRequest.DOWNLOAD_PATH
                              + dm.getFileName()
                              + MyDownloadFileRequest.TEMP_FILE_EXT_NAME;
        File tempFile = new File(tempFileName);

        // 下载好的文件
        String fileName = MyDownloadFileRequest.DOWNLOAD_PATH
                          + dm.getFileName();
        File file = new File(fileName);
        holder.btnContinue.setText(Util.getPercentFormat(0, dm.getTotal()));
        holder.btnPause.setText(Util.getPercentFormat(0, dm.getTotal()));
        // 如果临时文件存在，则显示“继续”按钮
        if (tempFile.exists())
        {
            holder.btnContinue.setText(Util.getPercentFormat(
                (int) tempFile.length(), dm.getTotal()));
            holder.btnPause.setText(Util.getPercentFormat(
                (int) tempFile.length(), dm.getTotal()));
            showSpecialBtn(holder.btnContinueArea, holder);
        }
        // 如果已经下载好，则显示“打开”按钮
        else if (file.exists())
        {
            holder.btnContinue.setText(Util.getPercentFormat(dm.getTotal(),
                dm.getTotal()));
            holder.btnPause.setText(Util.getPercentFormat(dm.getTotal(),
                dm.getTotal()));
            showSpecialBtn(holder.btnOpenArea, holder);
        }
        // 如果存在下载任务，并且下载任务正在进行，则显示“暂停”按钮
        else if (dm.getDownloadRequest() != null
                 && !dm.getDownloadRequest().hasPaused())
        {
            showSpecialBtn(holder.btnPauseArea, holder);
        }
        // 其他情况显示“下载”按钮
        else
        {
            showSpecialBtn(holder.btnDownloadArea, holder);
        }

        return convertView;
    }


    private void showSpecialBtn(LinearLayout showBtnArea,
            DownloadAdapter.ViewHolder holder)
    {
        holder.btnContinueArea.setVisibility(View.GONE);
        holder.btnDownloadArea.setVisibility(View.GONE);
        holder.btnPauseArea.setVisibility(View.GONE);
        holder.btnOpenArea.setVisibility(View.GONE);
        showBtnArea.setVisibility(View.VISIBLE);
    }

    public static class ViewHolder
    {
        public TextView title;
        public TextView btnOpen;
        public TextView btnPause;
        public TextView btnContinue;
        public TextView btnDownload;
        public LinearLayout btnOpenArea;
        public LinearLayout btnPauseArea;
        public LinearLayout btnContinueArea;
        public LinearLayout btnDownloadArea;
    }
}
